package com.haier.cre.commonframe.weight.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;

import com.blankj.utilcode.util.ScreenUtils;
import com.haier.cre.commonframe.R;
import com.haier.cre.commonframe.utils.CommonUtils;
import com.haier.cre.commonframe.utils.UIUtils;
import com.haier.cre.commonframe.weight.dialog.listener.DialogViewListener;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.ViewHolder;

/**
 * 项目名：CommonLibrary
 * 包 名：com.haier.cre.commonframe.weight.dialog
 * 创建人：18002063
 * 日 期：2018/10/16
 * 简 述：Dialog的工具类
 */
public class LibDialogUtils {

    public static final int TOP = Gravity.TOP;
    public static final int CENTER = Gravity.CENTER;
    public static final int BOTTOM = Gravity.BOTTOM;

    public static ProgressDialog showProgressDialog(Context context, DialogInterface.OnClickListener listener) {
        ProgressDialog dlg = new ProgressDialog(context);
        dlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dlg.setTitle(UIUtils.getString(R.string.LibDialogUtils_ProgressDialog_title));
        dlg.setMessage(UIUtils.getString(R.string.LibDialogUtils_ProgressDialog_content));
        dlg.setIndeterminate(false);
        dlg.setCanceledOnTouchOutside(false);
        dlg.setButton(UIUtils.getString(R.string.LibDialogUtils_ProgressDialog_cancel), listener);
        dlg.setCancelable(true);
        dlg.show();
        return dlg;
    }

    /**
     * 显示提示对话框
     * PermissionUtils
     */
    public static void showPermissionTipsDialog(final Activity activity) {
        new AlertDialog.Builder(activity)
                .setTitle(UIUtils.getString(R.string.LibDialogUtils_PermissionTipsDialog_title))
                .setMessage(UIUtils.getString(R.string.LibDialogUtils_PermissionTipsDialog_content))
                .setPositiveButton(UIUtils.getString(R.string.LibDialogUtils_PermissionTipsDialog_ok), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //                        PermissionUtils.startAppSettings(activity);
                        CommonUtils.getAppDetailSettingIntent(activity);
                    }
                }).show();
    }

    /**
     * 创建自定义Dialog,位置可以是 中间,顶部,底部
     *
     * @param context            上下文
     * @param viewResourceId     布局文件
     * @param gravity            位置 LibDialogUtils.TOP ,LibDialogUtils.CENTER ,LibDialogUtils.BOTTOM
     * @param isCancelable       返回按钮是否生效
     * @param dialogViewListener 布局文件的回调,添加自定义布局的点击事件等
     */
    public static DialogPlus showDialogBase(Context context, int viewResourceId, int gravity, boolean isCancelable,
                                            DialogViewListener dialogViewListener) {
        ViewHolder holder = new ViewHolder(viewResourceId);
        final DialogPlus dialog = DialogPlus.newDialog(context)
                .setContentHolder(holder)
                .setGravity(gravity)
                //                .setOnBackPressListener(backPressListener)
                //                .setOnClickListener(clickListener)
                .setExpanded(false)
                //                .setContentBackgroundResource(R.drawable.dialog_corner_bg_shape)
                .setCancelable(isCancelable)
                .setContentWidth((4 * ScreenUtils.getScreenWidth()) / 5)
                .create();

        dialogViewListener.onView(dialog, holder.getInflatedView());

        dialog.show();

        return dialog;
        //                .setOverlayBackgroundResource(android.R.color.transparent) //设置Dialog以外覆盖的背景
        //                .setContentBackgroundResource(android.R.color.transparent) //设置Dialog的背景
        //                .setContentHeight(500) //设置Dialog的高度
        //                .setContentWidth(500)  //设置Dialog的宽度
    }
}
